const News = require('../models/newsModel');

class NewsController {
    static async createNews(req, res) {
        try {
            const { title, content, reporter_name } = req.body;

            const newsId = await News.create({
                title,
                content,
                reporter_name
            });

            res.status(201).json({
                success: true,
                message: 'News created successfully',
                newsId
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: 'Error creating news',
                error: error.message
            });
        }
    }

    static async getAllNews(req, res) {
        try {
            const page = parseInt(req.query.page) || 1;
            const limit = parseInt(req.query.limit) || 10;

            const news = await News.getAll(page, limit);
            res.json({
                success: true,
                data: news
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: 'Error fetching news',
                error: error.message
            });
        }
    }

    static async getNewsById(req, res) {
        try {
            const newsId = req.params.id;
            const news = await News.findById(newsId);

            if (!news) {
                return res.status(404).json({
                    success: false,
                    message: 'News not found'
                });
            }

            
            await News.updateCounts(newsId, 'read_count');

            res.json({
                success: true,
                data: news
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: 'Error fetching news',
                error: error.message
            });
        }
    }

    static async updateNews(req, res) {
        try {
            const newsId = req.params.id;
            const { title, content, reporter_name } = req.body;

            
            const news = await News.findById(newsId);
            if (!news) {
                return res.status(404).json({
                    success: false,
                    message: 'News not found'
                });
            }

            const updated = await News.update(newsId, { title, content, reporter_name });
            if (updated) {
                res.json({
                    success: true,
                    message: 'News updated successfully'
                });
            } else {
                res.status(400).json({
                    success: false,
                    message: 'Failed to update news'
                });
            }
        } catch (error) {
            res.status(500).json({
                success: false,
                message: 'Error updating news',
                error: error.message
            });
        }
    }

    static async deleteNews(req, res) {
        try {
            const newsId = req.params.id;

            const news = await News.findById(newsId);
            if (!news) {
                return res.status(404).json({
                    success: false,
                    message: 'News not found'
                });
            }

            const deleted = await News.delete(newsId);
            if (deleted) {
                res.json({
                    success: true,
                    message: 'News deleted successfully'
                });
            } else {
                res.status(400).json({
                    success: false,
                    message: 'Failed to delete news'
                });
            }
        } catch (error) {
            res.status(500).json({
                success: false,
                message: 'Error deleting news',
                error: error.message
            });
        }
    }
}

module.exports = NewsController; 